package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.HttpHelper;
import com.wing.common.utils.JsonResult;
import com.wing.config.BaseDataInit;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.order.dao.DistributionDemandDao;
import com.wing.order.model.entity.DistributionDemand;
import com.wing.order.model.entity.DistributionDemandDetail;
import com.wing.order.model.entity.DistributionDemandPlan;
import com.wing.order.model.request.DistributionDemandDetailForm;
import com.wing.order.model.request.DistributionDemandForm;
import com.wing.order.model.response.DistributionDemandDetailVO;
import com.wing.order.model.response.DistributionDemandVO;
import com.wing.platform.feign.PlatformClient;
import com.wing.product.model.entity.Product;
import com.wing.product.model.entity.ProductSkuDetail;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.service.ProductDetailService;
import com.wing.product.service.ProductService;
import com.wing.product.service.ProductSkuDetailService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

import static java.lang.System.out;

/**
 * 配货需求订单表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DistributionDemandServiceImpl extends ServiceImpl<DistributionDemandDao, DistributionDemand> implements DistributionDemandService {


    @Autowired
    private DistributionDemandDetailService distributionDemandDetailService;

    @Autowired
    private DistributionDemandPlanService distributionDemandPlanService;

    @Autowired
    private ProductSkuDetailService productSkuDetailService;

    @Autowired
    private EnterpriseClient enterpriseClient;

    @Value("${platform.gatewayUrl}")
    private String gatewayUrl;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private PlatformClient platformClient;

    @Autowired
    private ProductDetailService productDetailService;

    @Autowired
    private ProductService productService;

    private MathContext mathContext = new MathContext(20, RoundingMode.HALF_UP);

    private static final String REQURL_DIS = "platformCab/order/admin/distributionDemand/addOrder";

    @Override
    public int add(DistributionDemandForm distributionDemandForm) {

        //校验

        DistributionDemand distributionDemand = new DistributionDemand();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(DistributionDemand.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(distributionDemandForm, distributionDemand, copyOptions);

        return baseMapper.insert(distributionDemand);
    }

    @Override
    @Transactional
    public JsonResult addList(List<DistributionDemandDetailForm> distributionDemandDetailForms) throws Exception {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (distributionDemandDetailForms.size() <= 0) {
            throw new BusinessException("配货需求添加失败，请选择要添加的配货记录");
        }
        Calendar cal = Calendar.getInstance();
        String date = cal.get(Calendar.YEAR) + String.valueOf(cal.get(Calendar.MONTH)) + cal.get(Calendar.DATE);
        //查询当天有无生成过配货单
        QueryWrapper<DistributionDemand> queryWrapper = new QueryWrapper();
        queryWrapper.like("dist_code", "PHD".concat(date));
        List<DistributionDemand> demandList = this.list(queryWrapper);
        Integer maxNo = 0;
        if (demandList.size() > 0) {
            maxNo = demandList.size();
        }
        DistributionDemand distributionDemand = new DistributionDemand();
        //刀具补货数量
        Long addNum = 0L;
        Long cabNum = 0L;
        Long cabSkuNum = 0L;
        BigDecimal total = new BigDecimal(0);
        List<DistributionDemandDetail> dfList = new ArrayList<>();
        for (DistributionDemandDetailForm distributionDemandDetailForm : distributionDemandDetailForms) {
            maxNo = maxNo + 1;
            //如果确认补货数累加
            if (Objects.isNull(distributionDemandDetailForm.getQuantityConfirm()) || distributionDemandDetailForm.getQuantityConfirm() == 0) {
                throw new BusinessException("配货需求添加失败，确认补货数不能为空！");
            }
            addNum += distributionDemandDetailForm.getQuantityConfirm();

            //查询商品价格
            QueryWrapper<ProductSkuDetail> querySkuWrapper = new QueryWrapper<>();
            querySkuWrapper.eq("sku_code", distributionDemandDetailForm.getSkuCode());
            ProductSkuDetail skuDetail = productSkuDetailService.getOne(querySkuWrapper);
            if (Objects.isNull(skuDetail)) {
                throw new BusinessException("未查询到产品数据,产品SKU编码为:" + distributionDemandDetailForm.getSkuCode());
            }

            //total = total.add(skuDetail.getSupplyPrice());
            ////计算货值
            //distributionDemandDetailForm.setTotalDetail(skuDetail.getSupplyPrice().multiply(new BigDecimal(distributionDemandDetailForm.getQuantityConfirm())));
            //distributionDemandDetailForm.setPrice(skuDetail.getSupplyPrice());
            //分析出有多少刀柜
            Product product = productService.getById(skuDetail.getProductId());
            if (Objects.isNull(product)) {
                throw new BusinessException("未查询到产品数据,产品ID为:" + skuDetail.getProductId());
            }

            if (Product.Type.knife.getValue().equals(product.getType().getValue())) {
                cabSkuNum = cabSkuNum + 1;
            } else if (Product.Type.cabinet.getValue().equals(product.getType().getValue())) {
                cabNum = cabNum + 1;
            }

            distributionDemandDetailForm.setReplenishmentNum(distributionDemandDetailForm.getQuantityConfirm());


            DistributionDemandPlan distributionDemandPlan = distributionDemandPlanService.getById(distributionDemandDetailForm.getPlanId());
            if (Objects.isNull(distributionDemandPlan)) {
                throw new BusinessException("未查询到配货计划");
            }

            if (DistributionDemandPlan.Status.replay.getValue() != distributionDemandPlan.getStatus().getValue()) {
                throw new BusinessException("配货计划不是待生成状态!");
            }

            distributionDemandPlan.setStatus(DistributionDemandPlan.Status.done);
            distributionDemandPlanService.updateById(distributionDemandPlan);

            DistributionDemandDetail demand = new DistributionDemandDetail();
            BeanUtil.copyProperties(distributionDemandDetailForm, demand);
            dfList.add(demand);

        }
        distributionDemand.setAddNum(addNum);
        distributionDemand.setCabNum(cabNum);
        distributionDemand.setCabSkuNum(cabSkuNum);
        distributionDemand.setTotal(total);
        //增长code
        String code = StringUtils.leftPad(String.valueOf(maxNo), 4, "0");

        distributionDemand.setDistCode("PHD".concat(date).concat(code));
        distributionDemand.setCode("PHD".concat(date).concat(code));
        distributionDemand.setStatus(DistributionDemand.Status.pending);
//        distributionDemand.setEnterpriseCode(dynamicService.getEnterpriseCode());
//        distributionDemand.setEnterpriseId(Long.valueOf(dynamicService.getEnterpriseId()));
        distributionDemand.setEnterpriseCode(enterpriseCode);
        distributionDemand.setEnterpriseId(enterpriseId);
        distributionDemand.setNo(Integer.valueOf(code));

        if (!this.saveOrUpdate(distributionDemand)) {
            throw new BusinessException("配货需求添加失败");
        }

        dfList.forEach(data -> {
            data.setDemandId(distributionDemand.getId());
            data.setDistributeCode(distributionDemand.getDistCode());
        });

        if (!distributionDemandDetailService.saveOrUpdateBatch(dfList)) {
            throw new BusinessException("配货需求明细添加失败");
        }
        distributionDemand.setDfList(dfList);
        String jsonObjectd = JSONUtil.toJsonStr(distributionDemand);
        String tokenJsons = HttpHelper.httpPost(gatewayUrl + REQURL_DIS, jsonObjectd);
        out.println(String.format("==========================>this is response:{%s}", tokenJsons));
        if (null == tokenJsons) {
            throw new BusinessException("数据同步失败,失败编码,返回数据为空!" );
        }
        JSONObject jsonObject = JSONObject.parseObject(tokenJsons);
        if (jsonObject.getInteger("code") != 200) {
            throw new BusinessException("数据同步失败,失败编码:" + jsonObject.getInteger("code")+":"+jsonObject.getString("msg"));
        }

        return null;
    }


    @Override
    public List<DistributionDemandVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DistributionDemandVO> pageList4app(Page page, Map<String, Object> params) {
        List<DistributionDemandVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<DistributionDemandVO> pageListReceive4app(Page page, Map<String, Object> params) {
        List<DistributionDemandVO> list = baseMapper.listReceive4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DistributionDemandVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult<ProductPackLabelVO> getPackLabel(Long id, Long detailId, String labelCode) {
        DistributionDemandVO distributionDemand = baseMapper.get4appById(id);
        if (distributionDemand == null) {
            return JsonResult.fail("未找到配货需求单业务数据");
        }
        JsonResult<ProductPackLabelVO> jsonResult = enterpriseClient.getProductPackLabelVOList(labelCode,0);
        ProductPackLabelVO labels = jsonResult.getData();
        if (labels == null) {
            return JsonResult.fail("未找到包装条码数据");
        }
        if (detailId != null) {
            DistributionDemandDetailVO demandDetailVO = distributionDemandDetailService.get4appById(detailId);
            if (demandDetailVO == null) {
                return JsonResult.fail("未找到业务明细数据");
            }

            if (!labels.getSkuCode().equals(demandDetailVO.getSkuCode())) {
                return JsonResult.fail("此包装编码的产品SKU[" + labels.getSkuCode() + "]不属于该产品SKU[" + demandDetailVO.getSkuCode() + "]");
            }
            labels.setDemandDetailId(demandDetailVO.getId());
            labels.setDemandId(demandDetailVO.getDemandId());
            labels.setQuantityReceive(demandDetailVO.getQuantityReceive());
            labels.setQuantityConfirm(demandDetailVO.getQuantityConfirm().intValue());
            labels.setDemandCode(distributionDemand.getDistCode());
            labels.setSkuPic(demandDetailVO.getSkuPic());
            labels.setSkuName(demandDetailVO.getSkuName());
            labels.setSkuName(demandDetailVO.getSkuName());
            labels.setSkuSpec(demandDetailVO.getSkuSpec());
            labels.setMarketPrice(demandDetailVO.getMarketPrice());
            labels.setProductTypeName(demandDetailVO.getProductTypeName());
            labels.setProductCategoryName(demandDetailVO.getProductCategoryName());
            labels.setProductBrandName(demandDetailVO.getProductBrandName());
            labels.setUnitName(demandDetailVO.getUnitName());
            if (labels.getSurplusQuantity() == 0) {
                return JsonResult.fail("此包装条码已领货出库");
            }
            return JsonResult.success(labels);
        }
        if (id != null) {
            Map paramMap = new HashMap();
            paramMap.put("demandId", id);
            List<DistributionDemandDetailVO> demandDetails = distributionDemandDetailService.list4app(paramMap);
            if (demandDetails.size() == 0) {
                return JsonResult.fail("未找到此领货单的业务明细数据");
            }
            StringBuffer skuCodes = new StringBuffer();
            Map<String, DistributionDemandDetailVO> demandeDetailIdMap = new HashMap<>();
            demandDetails.forEach(item -> {
                skuCodes.append(item.getSkuCode() + ",");
                demandeDetailIdMap.put(item.getSkuCode(), item);
            });
            if (!skuCodes.toString().contains(labels.getSkuCode())) {
                return JsonResult.fail("此包装编码的产品SKU[" + labels.getSkuCode() + "]不属于该领货单素有产品SKU[" + skuCodes.toString().substring(0, skuCodes.toString().length() - 1) + "]");
            }
            labels.setDemandDetailId(demandeDetailIdMap.get(labels.getSkuCode()).getId());
            labels.setDemandId(demandeDetailIdMap.get(labels.getSkuCode()).getDemandId());
            labels.setQuantityReceive(demandeDetailIdMap.get(labels.getSkuCode()).getQuantityReceive());
            labels.setQuantityConfirm(demandeDetailIdMap.get(labels.getSkuCode()).getQuantityConfirm().intValue());
            labels.setDemandCode(distributionDemand.getDistCode());
            labels.setSkuPic(demandeDetailIdMap.get(labels.getSkuCode()).getSkuPic());
            labels.setSkuName(demandeDetailIdMap.get(labels.getSkuCode()).getSkuName());
            labels.setSkuName(demandeDetailIdMap.get(labels.getSkuCode()).getSkuName());
            labels.setSkuSpec(demandeDetailIdMap.get(labels.getSkuCode()).getSkuSpec());
            labels.setMarketPrice(demandeDetailIdMap.get(labels.getSkuCode()).getMarketPrice());
            labels.setProductTypeName(demandeDetailIdMap.get(labels.getSkuCode()).getProductTypeName());
            labels.setProductCategoryName(demandeDetailIdMap.get(labels.getSkuCode()).getProductCategoryName());
            labels.setProductBrandName(demandeDetailIdMap.get(labels.getSkuCode()).getProductBrandName());
            labels.setUnitName(demandeDetailIdMap.get(labels.getSkuCode()).getUnitName());
            if (labels.getSurplusQuantity() == 0) {
                return JsonResult.fail("此包装条码已领货出库");
            }
            return JsonResult.success(labels);
        }
        return JsonResult.fail("获取包装条码失败");
    }

    @Override
    @Transactional
    public void save(DistributionDemandForm distributionDemandForm) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        DistributionDemand distributionDemand = new DistributionDemand();
        BeanUtils.copyProperties(distributionDemandForm, distributionDemand);
        String code = dynamicService.getDayNextCode("ord_distribution_demand");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String day = simpleDateFormat.format(new Date());
        distributionDemand.setDay(Integer.valueOf(day));
        distributionDemand.setDistCode(day + "-" + code);
        distributionDemand.setNo(Integer.valueOf(code));
        distributionDemandForm.setEnterpriseCode(enterpriseCode);
        distributionDemandForm.setEnterpriseId(Long.valueOf(enterpriseId));
        distributionDemand.setCreator(JwtUtils.getLoginUserId());
        distributionDemand.setCreatorName(JwtUtils.getLoginUsername());
        distributionDemand.setTotal(getTotal(distributionDemandForm));
        distributionDemand.setStatus(DistributionDemand.Status.created);
        baseMapper.insert(distributionDemand);

        distributionDemandForm.setId(distributionDemand.getId());
        distributionDemandForm.setDistCode(distributionDemand.getDistCode());
        distributionDemandForm.setTotal(distributionDemand.getTotal());
        if (CollectionUtils.isNotEmpty(distributionDemandForm.getDetailList())) {
            for (DistributionDemandDetailForm distributionDemandDetailForm : distributionDemandForm.getDetailList()) {
                DistributionDemandDetail distributionDemandDetail = new DistributionDemandDetail();
                BeanUtils.copyProperties(distributionDemandDetailForm, distributionDemandDetail);
                distributionDemandDetail.setId(null);
                distributionDemandDetail.setDemandId(distributionDemand.getId());
                BigDecimal total = new BigDecimal(0);
                if (distributionDemandDetailForm.getPrice() != null && distributionDemandDetailForm.getQuantityConfirm() != null) {
                    total = distributionDemandDetailForm.getPrice().multiply(BigDecimal.valueOf(distributionDemandDetailForm.getQuantityConfirm()), mathContext);
                } else {
                    throw new BusinessException("配货参数异常");
                }
                distributionDemandDetail.setTotalDetail(total);
                distributionDemandDetailService.save(distributionDemandDetail);
                distributionDemandDetailForm.setId(distributionDemandDetail.getId());
                distributionDemandDetailForm.setTotalDetail(total);
            }
        } else {
            throw new BusinessException("缺少配货需求明细");
        }
//        com.wing.platform.order.model.request.DistributionDemandForm distributionDemand4Platform=constructPlatformDemandData(distributionDemandForm);
//        JsonResult jsonResult=platformClient.synDistributionDemand(distributionDemand4Platform);
//        if (JsonResult.Code.SUCCESS.getCode()!=jsonResult.getCode()){
//            throw new BusinessException("数据同步至平台失败");
//        }
    }

    @Override
    @Transactional
    public void update(DistributionDemandForm distributionDemandForm) {
        DistributionDemand distributionDemand = new DistributionDemand();
        BeanUtils.copyProperties(distributionDemandForm, distributionDemand);
        distributionDemand.setModifier(JwtUtils.getLoginUserId());
        distributionDemand.setModifierName(JwtUtils.getLoginUsername());
        distributionDemand.setTotal(getTotal(distributionDemandForm));
        distributionDemand.setStatus(DistributionDemand.Status.created);
        baseMapper.updateById(distributionDemand);

//        distributionDemandForm.setId(distributionDemand.getId());
//        distributionDemandForm.setDistCode(distributionDemand.getDistCode());
//        distributionDemandForm.setTotal(distributionDemand.getTotal());
        distributionDemandDetailService.remove(new QueryWrapper<DistributionDemandDetail>().eq("demand_id", distributionDemandForm.getId()));
        if (CollectionUtils.isNotEmpty(distributionDemandForm.getDetailList())) {
            for (DistributionDemandDetailForm distributionDemandDetailForm : distributionDemandForm.getDetailList()) {
                DistributionDemandDetail distributionDemandDetail = new DistributionDemandDetail();
                BeanUtils.copyProperties(distributionDemandDetailForm, distributionDemandDetail);
                distributionDemandDetail.setId(null);
                distributionDemandDetail.setDemandId(distributionDemand.getId());
                BigDecimal total = new BigDecimal(0);
                if (distributionDemandDetailForm.getPrice() != null && distributionDemandDetailForm.getQuantityConfirm() != null) {
                    total = distributionDemandDetailForm.getPrice().multiply(BigDecimal.valueOf(distributionDemandDetailForm.getQuantityConfirm()), mathContext);
                } else {
                    throw new BusinessException("配货参数异常");
                }
                distributionDemandDetail.setTotalDetail(total);
                distributionDemandDetailService.save(distributionDemandDetail);
                distributionDemandDetailForm.setId(distributionDemandDetail.getId());
                distributionDemandDetailForm.setTotalDetail(total);
            }
        } else {
            throw new BusinessException("缺少配货需求明细");
        }
//        com.wing.platform.order.model.request.DistributionDemandForm distributionDemand4Platform=constructPlatformDemandData(distributionDemandForm);
//        JsonResult jsonResult=platformClient.synDistributionDemand(distributionDemand4Platform);
//        if (JsonResult.Code.SUCCESS.getCode()!=jsonResult.getCode()){
//            throw new BusinessException("数据同步至平台失败");
//        }
    }

    private BigDecimal getTotal(DistributionDemandForm distributionDemandForm) {
        BigDecimal sum = new BigDecimal(0);
        if (CollectionUtils.isNotEmpty(distributionDemandForm.getDetailList())) {
            for (DistributionDemandDetailForm distributionDemandDetailForm : distributionDemandForm.getDetailList()) {
                BigDecimal total = new BigDecimal(0);
                if (distributionDemandDetailForm.getPrice() != null && distributionDemandDetailForm.getQuantityConfirm() != null) {
                    total = distributionDemandDetailForm.getPrice().multiply(BigDecimal.valueOf(distributionDemandDetailForm.getQuantityConfirm()), mathContext);
                } else {
                    throw new BusinessException("配货参数异常");
                }
                sum = sum.add(total);
            }
        }
        return sum;
    }

    private com.wing.platform.order.model.request.DistributionDemandForm constructPlatformDemandData(DistributionDemandForm distributionDemandForm) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        com.wing.platform.order.model.request.DistributionDemandForm distributionDemand4Platform = new com.wing.platform.order.model.request.DistributionDemandForm();
        BeanUtils.copyProperties(distributionDemandForm, distributionDemand4Platform);
        distributionDemand4Platform.setEnterpriseId(enterpriseId);
        distributionDemand4Platform.setEnterpriseCode(enterpriseCode);
        distributionDemand4Platform.setStatus(com.wing.platform.order.model.entity.DistributionDemand.Status.replay_comfire);
        distributionDemand4Platform.setCode(distributionDemandForm.getDistCode());
//        distributionDemand4Platform.setTotal(distributionDemandForm.getTotal());
        List<com.wing.platform.order.model.request.DistributionDemandDetailForm> detailList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(distributionDemandForm.getDetailList())) {
            for (DistributionDemandDetailForm distributionDemandDetailForm : distributionDemandForm.getDetailList()) {
                com.wing.platform.order.model.request.DistributionDemandDetailForm distributionDemandDetail4Platform = new com.wing.platform.order.model.request.DistributionDemandDetailForm();
                BeanUtils.copyProperties(distributionDemandDetailForm, distributionDemandDetail4Platform);
                distributionDemandDetail4Platform.setStatus(com.wing.platform.order.model.entity.DistributionDemandDetail.Status.replay_comfire);
                distributionDemandDetail4Platform.setSkuSpecValueNames(distributionDemandDetailForm.getSkuSpec());
                distributionDemandDetail4Platform.setReplenishmentNum(distributionDemandDetailForm.getQuantityConfirm());
                distributionDemandDetail4Platform.setTotal(distributionDemandDetailForm.getTotalDetail());
                detailList.add(distributionDemandDetail4Platform);
            }
        }
        distributionDemand4Platform.setDetailList(detailList);
        return distributionDemand4Platform;
    }

    @Override
    @Transactional
    public JsonResult audit(Long id, DistributionDemand.Status status, String auditRemark) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String enterpriseName=JwtUtils.getEnterpriseName();
        DistributionDemand distributionDemand = new DistributionDemand();
        distributionDemand.setId(id);
        distributionDemand.setStatus(status);
        distributionDemand.setAuditRemark(auditRemark);
        distributionDemand.setAuditorId(JwtUtils.getLoginUserId());
        distributionDemand.setAuditorName(JwtUtils.getLoginUsername());
        distributionDemand.setAuditTime(LocalDateTime.now());
        baseMapper.updateById(distributionDemand);
        if (DistributionDemand.Status.pending.equals(status)) {
            DistributionDemandVO distributionDemandVO = baseMapper.get4appById(id);
            if (distributionDemandVO == null) {
                throw new BusinessException("未找到配货需求业务数据");
            }
            Map paramMap = new HashMap();
            paramMap.put("distributionDemandId", id);
            List<ProductDetailVO> detailList = productDetailService.list4app(paramMap);
            if (detailList.size() == 0) {
                throw new BusinessException("未找到配货需求明细数据");
            }
            com.wing.platform.order.model.request.DistributionDemandForm distributionDemand4Platform = new com.wing.platform.order.model.request.DistributionDemandForm();
            BeanUtils.copyProperties(distributionDemandVO, distributionDemand4Platform);
            distributionDemand4Platform.setEnterpriseId(enterpriseId);
            distributionDemand4Platform.setEnterpriseCode(enterpriseCode);
            distributionDemand4Platform.setCustomerName(enterpriseName);
            distributionDemand4Platform.setStatus(com.wing.platform.order.model.entity.DistributionDemand.Status.replay_comfire);
            distributionDemand4Platform.setCode(distributionDemandVO.getDistCode());

            List<com.wing.platform.order.model.request.DistributionDemandDetailForm> detaiFormlList = new ArrayList<>();
            detailList.forEach(item -> {
                com.wing.platform.order.model.request.DistributionDemandDetailForm distributionDemandDetail4Platform = new com.wing.platform.order.model.request.DistributionDemandDetailForm();
                BeanUtils.copyProperties(item, distributionDemandDetail4Platform);
                distributionDemandDetail4Platform.setStatus(com.wing.platform.order.model.entity.DistributionDemandDetail.Status.replay_comfire);
                distributionDemandDetail4Platform.setReplenishmentNum(item.getQuantityConfirm());
//                distributionDemandDetail4Platform.setSkuSpecValueNames(item.getSkuSpecValueNames());
//                distributionDemandDetail4Platform.setReplenishmentNum(item.getQuantityConfirm());
//                distributionDemandDetail4Platform.setTotal(item.getTotal()));
                detaiFormlList.add(distributionDemandDetail4Platform);
            });
            distributionDemand4Platform.setDetailList(detaiFormlList);

            JsonResult jsonResult = platformClient.synDistributionDemand(distributionDemand4Platform);
            if (JsonResult.Code.SUCCESS.getCode() != jsonResult.getCode()) {
                log.info("result code:" + jsonResult.getCode() + "");
                log.info("result msg:" + jsonResult.getMsg());
                log.error("syn fail", jsonResult.getCode(), jsonResult.getMsg());
                throw new BusinessException("数据同步至平台失败");
            }
            return JsonResult.success("审核成功");
        } else {
            return JsonResult.success("驳回成功");
        }
    }
}
